package com.lich.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

public class DateUtils {
	
	
	public static Date changeDay(Date date,int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.DAY_OF_MONTH, day);
		return calendar.getTime();
	}
	
	public static Date changeHour(Date date,int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.HOUR, +hour);
		return calendar.getTime();
		
	}

	public static Date addHours(Date date,int hour){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.HOUR, calendar.get(calendar.HOUR)+hour);
		return calendar.getTime();
	}
	public static Date addDays(Date date,int day){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.DAY_OF_YEAR, calendar.get(calendar.DAY_OF_YEAR)+day);
		return calendar.getTime();
	}
	
	public static Date strToDate(String str,String pattern){
		if (StringUtils.isBlank(str)) 
			return null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.parse(str);
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
		
	}
	/**
	 * 
	 * @param date
	 * @param pattern  默认为 yyyy-MM-dd
	 * @return
	 */
	public static String DateToStr(Date date,String pattern){
		if (date==null)
			return "";
		if (StringUtils.isBlank(pattern)) 
			pattern = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		return sdf.format(date);
	}
	
	

}
